﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class RoomDAO
    {
        private const string Success = "SUCCESS";
        private const string Fail = "FAIL: ";

        private BlueMDataContext _ctx;
        public RoomDAO()
        {
            _ctx = new BlueMDataContext();
        }

        public List<EquipmentDetail> SelectEquipment(int available)
        {
            IQueryable<EquipmentDetail> query = _ctx.EquipmentDetails;
            if (available > -1)
            {
                query = query.Where(p => p.Available == Convert.ToBoolean(available));
            }
            return query.ToList();
        }

        public List<EquipmentDetail> SelectEquipment(RoomType roomType)
        {
            IEnumerable<EquipmentDetail> equipmentDetails =
                roomType.Equipments.Select(equipment => equipment.EquipmentDetail);
            return equipmentDetails.ToList();
        }

        public EquipmentDetail SelectEquipmentByID(int id)
        {
            IQueryable<EquipmentDetail> query = _ctx.EquipmentDetails;
            query = query.Where(p => p.EquipmentDetailID == id);
            return query.FirstOrDefault();
        }

        public List<EquipmentDetail> SearchEquipmentByName(string name)
        {
            IQueryable<EquipmentDetail> query = _ctx.EquipmentDetails;
            query = query.Where(p => p.EquipmentName.Contains(name));
            return query.ToList();
        }

        public string InsertEquipment(EquipmentDetail equipmentDetail)
        {
            try
            {
                _ctx.EquipmentDetails.InsertOnSubmit(equipmentDetail);
                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public string UpdateEquipment(EquipmentDetail equipmentDetail)
        {
            try
            {
                EquipmentDetail e = SelectEquipmentByID(equipmentDetail.EquipmentDetailID);
                e.EquipmentName = equipmentDetail.EquipmentName;
                e.EquipmentPrice = equipmentDetail.EquipmentPrice;
                e.Available = equipmentDetail.Available;
                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }
        
        public RoomType SelectRoomTypeByID(int roomTypeID)
        {
            IQueryable<RoomType> query = _ctx.RoomTypes;
            query = query.Where(p => p.RoomTypeID == roomTypeID);
            return query.FirstOrDefault();
        }

        public List<RoomType> SelectRoomType()
        {
            IQueryable<RoomType> query = _ctx.RoomTypes;
            return query.ToList();
        }

        public string InsertRoomType(RoomType roomType, List<EquipmentDetail> equipmentDetails)
        {
            try
            {
                foreach (EquipmentDetail equipmentDetail in equipmentDetails)
                {
                    var e = new Equipment {EquipmentDetail = equipmentDetail};
                    roomType.Equipments.Add(e);
                }
                _ctx.RoomTypes.InsertOnSubmit(roomType);
                _ctx.SubmitChanges();

                return Success;
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public string UpdateRoomType(RoomType roomType, List<EquipmentDetail> equipmentDetails)
        {
            try
            {
                RoomType r = SelectRoomTypeByID(roomType.RoomTypeID);

                r.RoomPrice = roomType.RoomPrice;
                r.RoomRank = roomType.RoomRank;
                r.RoomTypeTitle = roomType.RoomTypeTitle;
                r.View = roomType.View;
                r.Balcony = roomType.Balcony;
                r.Bath = roomType.Bath;

                EntitySet<Equipment> equipments = r.Equipments;

                foreach (Equipment equipment in equipments)
                {
                    _ctx.Equipments.DeleteOnSubmit(equipment);
                }

                foreach (EquipmentDetail equipmentDetail in equipmentDetails)
                {
                    var e = new Equipment {EquipmentDetail = equipmentDetail};
                    r.Equipments.Add(e);
                }

                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {                

                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public List<Room> SelectRoom(int status)
        {
            IQueryable<Room> query = _ctx.Rooms;
            if (status <= 1 || status >= -1)
            {
                query = query.Where(p => p.Status == status);
            }
            return query.ToList();
        }

        public Room SelectRoomByID(int roomId)
        {
            IQueryable<Room> query = _ctx.Rooms;
            query = query.Where(p => p.RoomID == roomId);
            return query.FirstOrDefault();
        }

        public List<Room> SelectRoomByRoomTypeID(int roomTypeId)
        {
            RoomType roomType = SelectRoomTypeByID(roomTypeId);
            return roomType.Rooms.ToList();
        }

        public string InsertRoom(Room room)
        {
            try
            {
                _ctx.Rooms.InsertOnSubmit(room);
                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }

        public string UpdateRoom(Room room)
        {
            try
            {
                Room r = SelectRoomByID(room.RoomID);
                r.RoomTypeID = room.RoomTypeID;
                r.Status = room.Status;
                _ctx.SubmitChanges();
                return Success;
            }
            catch (Exception ex)
            {
                _ctx = new BlueMDataContext();
                return Fail + ex.Message;
            }
        }
    }
}